package satb.base.view;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import satb.base.Configuration;
import satb.base.controller.ConfigurationController;
import satb.controller.CollarDataController;
import satb.utilities.AlertDialog;

public class ConfigurationView implements Initializable {

	 private Stage subStage;
	 private Configuration configuration;
	 @FXML private Button btnSave;
	 @FXML private Button btnClose;
	 @FXML private TextField txtPath;
	 @FXML private TextField txtKML;
	
	public ConfigurationView()
	{	
		this.txtPath = new TextField();
		this.txtKML = new TextField();
		this.configuration = null;
	}

	 
	 
	public void execute()
    {
        subStage = new Stage();
        subStage.setTitle("Configurações");
        subStage.setResizable(false);
        
        try 
        {
            Parent root = FXMLLoader.load(getClass().getResource("fxml/Configuration.fxml"));
            Scene scene = new Scene(root);
        
            subStage.setScene(scene);
            subStage.show();
        } 
        catch (IOException ex) 
        {
            Logger.getLogger(CollarDataController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
	
	
	  @Override
      public void initialize(URL url, ResourceBundle rb) 
	  {
		  ConfigurationController config = new ConfigurationController();
		  try 
		  {
			  this.configuration = config.configurationSearch();
			  this.txtPath.setText(this.configuration.getGoogleEarthPath());
			  this.txtKML.setText(this.configuration.getKmlPath());
		  } 
		  catch (Exception e) 
		  {
			  e.printStackTrace();
		  }
      }  
	
	
	
	@FXML
	private void save(ActionEvent event) 
	{
		if(this.configuration == null)
		{
			this.configuration = new Configuration();
		}
		
		this.configuration.setGoogleEarthPath(this.txtPath.getText());
		this.configuration.setKmlPath(this.txtKML.getText());
		
		ConfigurationController config = new ConfigurationController();
		config.configurationSave(this.configuration);
		
		new AlertDialog((Stage) btnSave.getScene().getWindow(), "Save Sucessful!", AlertDialog.ICON_INFO).showAndWait();
	}

	
	@FXML
	private void close(ActionEvent event) 
	{
		// get a handle to the stage
		Stage stage = (Stage) btnClose.getScene().getWindow();
		// do what you have to do
		stage.close();
	}
	
}

